﻿using System;
using System.Linq;
using ReactiveGraph.Core;

namespace ReactiveGraph.Tests.Extensions
{
    static class NodeEventObservableExtensions
    {
        public static IObservable<IProperty> DescendantPropertyChanged(this IReactiveNode node)
        {
            return DescendantPropertyChanged(node.Events);
        }

        public static IObservable<IProperty> DescendantPropertyChanged(this IObservable<NodeEvent> events)
        {
            return events
                .Where(e => e.Type == EventType.PropertyChange)
                .Select(e => e.Source.Property);
        }

        public static IObservable<IReactiveNode> ChildOfParentAdded(this IObservable<NodeEvent> events, IReactiveNode parent)
        {
            return events
                .Where(e => e.Type == EventType.DescendantAdded && parent == e.Source.ParentNode)
                .Select(e => e.Source.Node);
        }

        public static IObservable<IReactiveNode> ChildOfParentRemoved(this IObservable<NodeEvent> events, IReactiveNode parent)
        {
            return events
                .Where(e => e.Type == EventType.DescendantRemoved && parent == e.Source.ParentNode)
                .Select(e => e.Source.Node);
        }
    }
}
